﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;

public partial class Customer_Product_List_Item : PageBase
{

    private const string REQUEST_LIST_ID = "listID";
    private int listID = 0;
    private const string RESPONSE_LIST_MAINTAIN_PAGE = "Customer_Product_List.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[REQUEST_LIST_ID] != null)
        {
            int.TryParse(Request.QueryString[REQUEST_LIST_ID], out listID);
        }
        if (!IsPostBack)
        {
            this.InitForm();
            ddlSelected.SelectedValue = "1";
            BindGrid();
            ddlSelected.SelectedValue = "0";
        }
    }

    protected void SelectALL(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox;
        if (chk == null)
            return;
        foreach (GridViewRow row in this.GridView1.Rows)
        {
            CheckBox checkbox = row.Cells[GridView1.Columns.Count - 1].FindControl("cbxSelected") as CheckBox;
            if (checkbox != null)
                checkbox.Checked = chk.Checked;
        }
    }


    private void InitForm()
    {
        CUSTOMER_PRODUCT_LISTS list = new CustomerProductListManager().Load(listID);
        if (list != null)
        {
           // lblListName.Text = list.NAME;
        }

        #region 绑定制造商

        ddlManu.DataSource = ManufacturersManager.GetManus(MyStateManager.DomainID,false);
        ddlManu.DataTextField = "ManuName";
        ddlManu.DataValueField = "ManuCode";
        ddlManu.DataBind();
        ddlManu.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));

        #endregion

        #region 绑定品类

        ddlCategroy.DataSource = CategoryManager.GetCategories(MyStateManager.DomainID);
        ddlCategroy.DataTextField = "CategoryName";
        ddlCategroy.DataValueField = "CategoryCode";
        ddlCategroy.DataBind();
        ddlCategroy.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));

        #endregion

        #region 绑定品牌

        ddlBrand.DataSource = BrandManager.GetBrands(MyStateManager.DomainID);
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        ddlBrand.DataBind();
        ddlBrand.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));

        #endregion

         #region 绑定系列
   
        ObjectTable ot;
        ddlForm.DataTextField = "FormName";
        ddlForm.DataValueField = "FormCode";
        ot = FormManager.GetFORMS(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=True";
            ddlForm.DataSource = ot.BillDataTable.DefaultView;
            ddlForm.DataBind();
        }
        ddlForm.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));
    
    #endregion

    }

    private void BindGrid()
    {
        if (listID > 0)
        {
            GridPager1.DataSource = CustomerProductListItemManager.GetProductListItem(listID,
                                                                                        ddlCategroy.SelectedValue,
                                                                                        ddlBrand.SelectedValue,
                                                                                        ddlManu.SelectedValue,
                                                                                        ddlForm.SelectedValue,
                                                                                        this.MyStateManager.DomainID,
                                                                                        ddlSelected.SelectedValue).Tables[0];
            GridPager1.GridView = GridView1;
            GridPager1.BindData();
            GridPager1.Visible = GridView1.Rows.Count > 0;

            
        }
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            double dbPiecePrice, dbDiscount = 0, dbCasePrice;

            DateTime dtStart = Convert.ToDateTime("1900-1-1");
            DateTime dtEnd = Convert.ToDateTime("1900-1-1");
            int itemID = 0, flag;

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                int productID = Convert.ToInt32(GridView1.DataKeys[i].Value);

                string PIECE_PRICE = ((TextBox)GridView1.Rows[i].FindControl("txtPiecePrice")).Text;
                double.TryParse(PIECE_PRICE, out dbPiecePrice);

                string stringflag= ((DropDownList)GridView1.Rows[i].FindControl("ddl_flag")).SelectedValue;
                int.TryParse(stringflag, out flag);

                string CASE_PRICE = ((TextBox)GridView1.Rows[i].FindControl("txtCasePrice")).Text;
                double.TryParse(CASE_PRICE, out dbCasePrice);

                var ctl = ((TextBox)GridView1.Rows[i].FindControl("txtTprDiscount"));
                if (ctl != null)
                {
                    string TPR_DISCOUNT = ctl.Text;
                    double.TryParse(TPR_DISCOUNT, out dbDiscount);
                }
                bool IsSelected = ((CheckBox)GridView1.Rows[i].FindControl("cbxSelected")).Checked;
                Control ctlStartDate = GridView1.Rows[i].FindControl("TxtStartDate");

                Control ctlItemID = GridView1.Rows[i].FindControl("lblItemID");
                if (ctlItemID != null)
                {
                    Label lblItemID = ctlItemID as Label;
                    if (lblItemID != null)
                    {
                        int.TryParse(lblItemID.Text, out itemID);
                    }
                }

                if (ctlStartDate != null)
                {
                    TextBox txtStart = ctlStartDate as TextBox;
                    if (txtStart != null && txtStart.Text != string.Empty)
                    {
                        DateTime.TryParse(txtStart.Text, out dtStart);
                    }
                }
                Control ctlEndDate = GridView1.Rows[i].FindControl("TxtStartDate");
                if (ctlEndDate != null)
                {
                    TextBox txtEnd = ctlEndDate as TextBox;
                    if (txtEnd != null && txtEnd.Text != string.Empty)
                    {
                        DateTime.TryParse(txtEnd.Text, out dtEnd);
                    }
                }

                if (IsSelected || (!IsSelected && itemID > 0))
                {
                    CustomerProductListManager.AssignProductListItem(IsSelected,
                                                                    itemID,
                                                                    this.listID,
                                                                    productID,
                                                                    dbPiecePrice,
                                                                    this.MyStateManager.Name,
                                                                    DateTime.Now,
                                                                    dbCasePrice,
                                                                    flag,
                                                                    dbDiscount,
                                                                    dtStart,
                                                                    dtEnd,
                                                                    MyStateManager.DomainID
                                                                    );
                }
            }

            ShowMessage("保存成功");
            
        }
        catch (Exception ex)
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
              Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
              this.MyStateManager.UserID, "编辑产品 - 异常:" + ex.Message);
        }
    }

    protected void ddlCategroy_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        ddlBrand.DataSource = BrandManager.GetBrandsListDaynamic(ddlCategroy.SelectedValue, ddlManu.SelectedValue, MyStateManager.DomainID);
        ddlBrand.DataBind();
        ddlBrand.Items.Insert(0, new ListItem(Resources.language.ddl_Select, ""));
    }
    protected void ddlManu_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        ddlBrand.DataSource = BrandManager.GetBrandsListDaynamic(ddlCategroy.SelectedValue, ddlManu.SelectedValue, MyStateManager.DomainID);
        ddlBrand.DataBind();
        ddlBrand.Items.Insert(0, new ListItem(Resources.language.ddl_Select, ""));
    }

    public void GridViewToolbarUserControl1_OnGVTBSetColumnsButtonEvent(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
          DropDownList ddl_flag= (DropDownList)e.Row.FindControl("ddl_flag");
          if (ddl_flag != null)
          {
              DataRowView dv = e.Row.DataItem as DataRowView;
              if(dv!=null)
                ddl_flag.SelectedValue =dv.Row["FLAG"].ToString();
          }
        }
    }
}

